package com.bosch.in.rtp1.ept.dao.impl;

import java.math.BigDecimal;
import java.sql.Array;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Struct;
import java.util.ArrayList;
import java.util.List;

import com.bosch.in.rtp1.ept.constant.ReleasedHoldParameter;
import com.bosch.in.rtp1.ept.dao.HldDurByMonDiagramDao;
import com.bosch.in.rtp1.ept.model.dto.BoxPlotFactor;
import com.bosch.in.rtp1.ept.model.dto.HldDurByMonth;
import com.bosch.in.rtp1.ept.model.technical.Condition4HldDurByMonthDiagram;
import com.bosch.in.rtp1.ept.util.ConverterUtility;
import com.bosch.in.rtp1.ept.util.SQLStatementLoader;

public class HldDurByMonDiagramJDBCDao extends GenericDao implements HldDurByMonDiagramDao{

	/**
	 * Constructor to inject connection
	 * @param con	DB connection
	 */
	public HldDurByMonDiagramJDBCDao(Connection connection) {
		super(connection);
	}

	@Override
	public List<HldDurByMonth> getHldDurChartByMonth(Condition4HldDurByMonthDiagram cByMonthDiagram) throws SQLException {
		String queryString = SQLStatementLoader.getHldDurByMonth();
		ResultSet resultSet = executeQuery(queryString,cByMonthDiagram.getParameterValues().toArray());
		List<HldDurByMonth> lDurByMonths = new ArrayList<HldDurByMonth>();
		while (resultSet.next())
		{
			HldDurByMonth cByMonth = new HldDurByMonth();
			cByMonth.setMonth(resultSet.getInt(ReleasedHoldParameter.MONTH_DB));
			cByMonth.setPriority(resultSet.getInt(ReleasedHoldParameter.PRIORITY_DB));
			List<BoxPlotFactor> bFactors = new ArrayList<BoxPlotFactor>();
			Array array = resultSet.getArray(ReleasedHoldParameter.FACTOR_LIST_DB);			
            Object[] factorList = (Object[]) array.getArray();  
            for (int i = 0; i < factorList.length; i++) {  
               Struct factor = (Struct) factorList[i];  
               Object[] attrib = factor.getAttributes();
               BoxPlotFactor bFactor = new BoxPlotFactor();
               bFactor.setSigmaTop		(ConverterUtility.bigDecimalToFloat((BigDecimal) attrib[ReleasedHoldParameter.SIGMA_TOP_IDX_DB]));
               bFactor.setSigmaBottom	(ConverterUtility.bigDecimalToFloat((BigDecimal) attrib[ReleasedHoldParameter.SIGMA_BOT_IDX_DB]));
               bFactor.setSigmaMiddle1	(ConverterUtility.bigDecimalToFloat((BigDecimal) attrib[ReleasedHoldParameter.SIGMA_MIDD_1_IDX_DB]));
               bFactor.setSigmaMiddle2	(ConverterUtility.bigDecimalToFloat((BigDecimal) attrib[ReleasedHoldParameter.SIGMA_MIDD_2_IDX_DB]));
               bFactor.setMedian		(ConverterUtility.bigDecimalToFloat((BigDecimal) attrib[ReleasedHoldParameter.MEDIAN_IDX_DB]));
               bFactor.setxLineValue	((String) attrib[ReleasedHoldParameter.HOLD_CODE_IDX_DB]);
               bFactor.setTotalItem		(((BigDecimal) attrib[ReleasedHoldParameter.COUNT_IDX_DB]).intValue());
               bFactors.add(bFactor);
            }  
            cByMonth.setBoxPlotFactors(bFactors);
            lDurByMonths.add(cByMonth);
		}
		
		// close statement, result set
		resultSet.close();
		close();
		return lDurByMonths;
	}
		
}
